package com.myhome.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.dto.CoinDto;
import com.myhome.mappers.CoinMappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myhome.domain.Coin;
import com.myhome.mapper.CoinMapper;
import com.myhome.service.CoinService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @Author QYiXin
 * @Date 2021 10 20 09 37
 **/
@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin> implements CoinService {

    @Override
    public Page<Coin> findByPage(String name, String type, Byte status, String title, String walletType, Page<Coin> page) {
        return page(page,
                new LambdaQueryWrapper<Coin>()
                        .like(!StringUtils.isEmpty(name), Coin::getName, name) // 名称的查询
                        .like(!StringUtils.isEmpty(title), Coin::getTitle, title)  // 标题的查询
                        .eq(status != null, Coin::getStatus, status)  // 状态的查询
                        .eq(!StringUtils.isEmpty(type), Coin::getType, type) // 货币类型名称的查询
                        .eq(!StringUtils.isEmpty(walletType), Coin::getWallet, walletType) // 货币钱包类型的查询
        );
    }

    @Override
    public List<Coin> getCoinsByStatus(Byte status) {
        return this.list(new LambdaQueryWrapper<Coin>().eq(Coin::getStatus, status));
    }

    @Override
    public List<CoinDto> getCoinDto(List<Long> ids) {
        List<Coin> coinList = this.list(new LambdaQueryWrapper<Coin>().in(ids != null, Coin::getId, ids));
        if (coinList == null) {
            return Collections.emptyList();
        }
        return CoinMappers.INSTANCE.convert2Dto(coinList);
    }

}
